#include <stdio.h>
#include <unistd.h>
#include <sys/shm.h>
#include <errno.h>

#include "share_data.h"
#include "shm_data.h"

share_data_t* shm_data_init(void)
{
    void *shm = NULL;  
    int shmid;  

    //创建共享内存  
    shmid = shmget((key_t)SHM_KEY, sizeof(share_data_t), 0666 | IPC_CREAT);  
    if(shmid == -1)  
    {  
        perror("shmget fail! info: ");
        goto fail;
    }  

    //将共享内存连接到当前进程的地址空间  
    shm = shmat(shmid, (void*)0, 0);  
    if(shm == (void*)-1)  
    {  
        perror("shmat fail! info: ");  
        goto fail;
    }  

    return (share_data_t *)shm;  

fail:
    return NULL;
}
